using System;

namespace SharpMud.Server.Systems.Security.Entities
{
	public class UserReference : EntityReference
	{
		#region Constructors
		public UserReference()
			:base()
		{
		}

		public UserReference(IUser user)
			:base((IEntity)user)
		{
		}

		public UserReference(string userName)
			:base(userName)
		{
		}

		public UserReference(string userName, System.Guid userID)
			:base(userName,userID)
		{
		}
		#endregion

		#region Overloads
		public override bool Equals(object obj)
		{
			if(obj.GetType().IsSubclassOf(typeof(UserReference)) || obj.GetType()==typeof(UserReference))
			{
				return Equals((UserReference)obj);
			}
			else if(obj.GetType().IsSubclassOf(typeof(EntityReference)) || obj.GetType()==typeof(EntityReference))
			{
				return ((EntityReference)this).Equals((EntityReference)obj);
			}
			else if(obj.GetType().GetInterface(typeof(IEntity).FullName,true)!=null)
			{
				return Equals(new EntityReference((IEntity)obj));
			}
			else if(obj.GetType().GetInterface(typeof(IUser).FullName,true)!=null)
			{
				return Equals(new UserReference((IUser)obj));
			}
			else
			{
				return base.Equals(obj);
			}
		}

		public override int GetHashCode()
		{
			return base.GetHashCode();
		}

		#region op_Equals
		public static bool operator==(UserReference obj1, UserReference obj2)
		{
			return(obj1.Equals(obj2));
		}

		public static bool operator==(UserReference obj1, object obj2)
		{
			return(obj1.Equals(obj2));
		}

		public static bool operator==(object obj1, UserReference obj2)
		{
			return(obj2.Equals(obj1));
		}

		public static bool operator==(UserReference obj1, IEntity obj2)
		{
			return(obj1.Equals(obj2));
		}

		public static bool operator==(IEntity obj1, UserReference obj2)
		{
			return(obj2.Equals(obj1));
		}
		#endregion
		#region op_NotEquals
		public static bool operator!=(UserReference obj1, UserReference obj2)
		{
			return(!obj1.Equals(obj2));
		}

		public static bool operator!=(UserReference obj1, object obj2)
		{
			return(!obj1.Equals(obj2));
		}

		public static bool operator!=(object obj1, UserReference obj2)
		{
			return(!obj2.Equals(obj1));
		}

		public static bool operator!=(UserReference obj1, IEntity obj2)
		{
			return(!obj1.Equals(obj2));
		}

		public static bool operator!=(IEntity obj1, UserReference obj2)
		{
			return(!obj2.Equals(obj1));
		}
		#endregion
		public bool Equals(UserReference obj)
		{
			if(
				this.Entity==null && this.ID==Guid.Empty && this.Name==String.Empty
				&&
				obj.Entity==null && obj.ID==Guid.Empty && obj.Name==String.Empty
				)
			{
				return true;
			}

			if((this.Entity==obj.Entity) && obj.Entity!=null)
				return true;
			if((this.Entity!=obj.Entity) 
				&& (this.Entity!=null) && (obj.Entity!=null))
				return false;
			
			if((this.ID==obj.ID) && obj.ID!=Guid.Empty)
				return true;
			if((this.ID!=obj.ID) 
				&& (this.ID!=Guid.Empty) && (obj.ID!=Guid.Empty))
				return false;

			if((this.Name==obj.Name) && obj.Name!=String.Empty)
				return true;
			if((this.Name!=obj.Name) 
				&& (this.Name!=String.Empty) && (obj.Name!=String.Empty))
				return false;

			return false;
		}
		#endregion
	}
}
